/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package moviefansystem.Gui;

import java.awt.Component;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import moviefansystem.Film;
import moviefansystem.Klant;
import moviefansystemControllers.Controller;
import moviefansystemControllers.GuiController;
/** 
 *
 * @author Nick
 */
public class Gui_KlantGevonden extends javax.swing.JFrame {
    private static Klant klant;
    ArrayList<Film> lijstGehuurd;
    ArrayList<Film> lijstInBezit;
    ArrayList<Film> lijstGereseveerd;
    Controller con;
    GuiController gCon;
    /**
     * Creates new form Gui_KlantGevonden
     */
    public Gui_KlantGevonden(GuiController gCon,Klant klant) {
        this.klant = klant;
        initComponents();
        con = new Controller();
        this.gCon = gCon;
 
        vulKlantGegevens(klant);
        vulReserveringen(klant);
        vulGehuurdeFilms(klant);     
    }

    private Gui_KlantGevonden(Klant klant) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    /**
     * functie om klant gegevns in overzicht te tonen
     * @param k klant waar het om gaat
     */
    public void vulKlantGegevens(Klant k)
    {
        TbGgevens.setValueAt("Naam: "+k.getNaam()+" "+k.getTussenvoegsel()+" "+k.getAchternaam(), 0, 0);
        TbGgevens.setValueAt("Telefoon: "+k.getTelefoonnr(), 1, 0);
        TbGgevens.setValueAt("Adres: "+k.getAdres(), 3, 0);
        TbGgevens.setValueAt("Postcode: "+k.getPostcode(), 4, 0);
        TbGgevens.setValueAt("Plaats: "+k.getWoonplaats(), 5, 0);
        TbGgevens.setValueAt("Bankrekening: "+k.getBankrekeningNr() , 7, 0);
    }
    /**
     * funcite om reservering gegevns in overzicht te tonen
     * @param k klant waar het om gaat
     */
    public void vulReserveringen(Klant k)
    {
     lijstGereseveerd = con.getPersItemsGereseveerd(k);
     
     int i = 0;
     while(i<lijstGereseveerd.size())
     {
        TbGgevens.setValueAt(lijstGereseveerd.get(i).getNaam()+" ArtikelNr: "+lijstGereseveerd.get(i).getArtiekelNr(), i, 1);
     i++;
     }
      
    }
    /**
     * functie om gehuurde films te vullen met ook huidige inbezit films
     * 
     * @param k de klant waar het om gaat
     */
    public void vulGehuurdeFilms(Klant k)
    {
      lijstInBezit = con.getPersItemsInbezit(k);
      TbGgevens.setValueAt("Films inbezit door Klant:", 0, 2);
      int i = 0;
      while(i < 3)
      {
        TbGgevens.setValueAt(lijstInBezit.get(i).getNaam()+" ArtikelNr: "+lijstInBezit.get(i).getArtiekelNr(), i+2, 2);  
        i++;
      }
      
      TbGgevens.setValueAt("Films ooit gehuurd:", 6, 2);
      lijstGehuurd = con.getPersItemsGehuurd(k);
      int j = 0;
      while(j <lijstGehuurd.size())
      {
          TbGgevens.setValueAt(lijstGehuurd.get(j).getNaam()+" ArtikelNr: "+lijstGehuurd.get(j).getArtiekelNr(), j+8, 2);
          j++;
      }
      
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        TbGgevens = new javax.swing.JTable();
        lbEror3 = new javax.swing.JPanel();
        BtInnemen = new javax.swing.JButton();
        BtReserveer = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        TfFilmId = new javax.swing.JTextField();
        BtVerhuur = new javax.swing.JButton();
        lbError1 = new javax.swing.JLabel();
        lberror2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btAnnuleer = new javax.swing.JButton();

        jTextField1.setText("jTextField1");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel8.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        TbGgevens.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        TbGgevens.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Klant Gegevens:", "Reserveringen:", "Geleende Films:"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        TbGgevens.setCellSelectionEnabled(true);
        TbGgevens.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(TbGgevens);
        TbGgevens.getColumnModel().getColumn(0).setResizable(false);
        TbGgevens.getColumnModel().getColumn(1).setResizable(false);
        TbGgevens.getColumnModel().getColumn(2).setResizable(false);

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 321, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(106, 106, 106))
        );

        lbEror3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        BtInnemen.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        BtInnemen.setText("Innemen");
        BtInnemen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtInnemenActionPerformed(evt);
            }
        });

        BtReserveer.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        BtReserveer.setText("Reserveer");
        BtReserveer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtReserveerActionPerformed(evt);
            }
        });

        jLabel1.setText("Film ID:");

        BtVerhuur.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        BtVerhuur.setText("Verhuur");
        BtVerhuur.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtVerhuurActionPerformed(evt);
            }
        });

        lbError1.setForeground(new java.awt.Color(255, 0, 0));

        lberror2.setForeground(new java.awt.Color(255, 0, 0));

        jLabel4.setForeground(new java.awt.Color(255, 0, 0));

        btAnnuleer.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        btAnnuleer.setText("Annuleer");
        btAnnuleer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAnnuleerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout lbEror3Layout = new javax.swing.GroupLayout(lbEror3);
        lbEror3.setLayout(lbEror3Layout);
        lbEror3Layout.setHorizontalGroup(
            lbEror3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lbEror3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(lbEror3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, lbEror3Layout.createSequentialGroup()
                        .addGroup(lbEror3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btAnnuleer, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(BtVerhuur, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(BtReserveer, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(lbEror3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(lbError1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lberror2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(lbEror3Layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                                .addGap(100, 100, 100))))
                    .addGroup(lbEror3Layout.createSequentialGroup()
                        .addGroup(lbEror3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(lbEror3Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(TfFilmId))
                            .addComponent(BtInnemen, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        lbEror3Layout.setVerticalGroup(
            lbEror3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lbEror3Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(BtInnemen, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbError1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(lbEror3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(TfFilmId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addComponent(lberror2)
                .addGroup(lbEror3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(lbEror3Layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(jLabel4))
                    .addGroup(lbEror3Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(BtReserveer, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BtVerhuur, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btAnnuleer, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbEror3, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
                    .addComponent(lbEror3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(57, 57, 57))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtVerhuurActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtVerhuurActionPerformed
        clearErrorLog();
        String buffer = TfFilmId.getText();//FIlm ID uit Gui halen
        int filmID = cheakFilmIdOpInter(buffer);
        if(filmID <0)
        {
            lbError1.setText("FilmId niet correct");
            lberror2.setText("(bijvoorbeeld \"1234\"");

        }
        else
        {
            if(con.filmVerhuren(klant.getKlantNr(), filmID))
            {
                 JOptionPane.showMessageDialog(this,"Film met ID "+filmID+" verhuurd voor "+klant.getNaam());
            }
            else
            {//@Todo
                lbError1.setText("Film kan niet " );
                lberror2.setText("worden verhuurd");
                //Show box Kan filmID.gettitel niet verhuren aan klant.getNaam();
            }
        }

    }//GEN-LAST:event_BtVerhuurActionPerformed

    private void BtInnemenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtInnemenActionPerformed
        clearErrorLog();
       
       int filmIdX = TbGgevens.getSelectedColumn();// haal de X ass op
       int filmIdY = TbGgevens.getSelectedRow();// haal de Y ass op
       // hier controleren of wel geseleteerd is uit gedeelte Klant inbezit.
       if(filmIdX !=2 || filmIdY <2 || filmIdY > 4)//tbv kijken of uit de juiste lijst wordt geselecteerd.
       {
        lbError1.setText("Selecteer uit de films");
        lberror2.setText("In bezit door Klant");
       }
       else
       {
       
       String buffer = (String)TbGgevens.getModel().getValueAt(filmIdY, filmIdX);// haal de String op uit juiste blok
       
       int i = 9;//minimaal plaatst waar staad dmv woord ArtikelNr
       char aChar = buffer.charAt(0);
       while(i<buffer.length())
       {
           aChar = buffer.charAt(i);// zoek het teken :
        
           if(aChar == ':' )//indien gevonden maak substring met het artikel nr.
           {
               buffer = buffer.substring(i+2);// +2 is om de spatie + : weg te halaen
               i = 1000;
           }
              i++;
       }
       int movieNr = Integer.parseInt(buffer);

       if(con.filmInnemen(klant.getKlantNr(), movieNr))
       {
           JOptionPane.showMessageDialog(this,"Film met ID "+buffer+" ingenomen"+klant.getNaam());
       } 
       else
       {
               lbError1.setText("Film niet ingenomen");
               lberror2.setText("oorzaak onbekend"); 
       }
       }

    }//GEN-LAST:event_BtInnemenActionPerformed
    /**
     * return int als Filmid indien mogelijk anders return -1
     * @param FilmId is string met nummer s
     * @return 
     */
    public int cheakFilmIdOpInter(String FilmId)
    {
        int filmInt;
        try
        {
            filmInt = Integer.parseInt(FilmId);
            return filmInt;
        }
        catch(Exception ex)
        {
            filmInt = -1;
            return filmInt;
        }
    }
    private void BtReserveerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtReserveerActionPerformed
        lbError1.setText("");
        String buffer = TfFilmId.getText(); //FIlm ID uit Gui halen
        int filmID = cheakFilmIdOpInter(buffer);
        if(filmID <0)
        {
            lbError1.setText("FilmId niet correct");
            lberror2.setText("(bijvoorbeeld \"1234\"");

        }
        else
        {
        
        if(con.filmReseveren(klant.getKlantNr(), filmID))
        {
               JOptionPane.showMessageDialog(this,"Film met ID "+filmID+" gereseveerd voor "+klant.getNaam());
        }
        else
        {//@Todo
            lbError1.setText("Film kan niet" );
            lberror2.setText("worden gereseveerd");
            //show box Film niet gereseveerd voor klant.getklantnaam();
        }
        }
        
    }//GEN-LAST:event_BtReserveerActionPerformed

   
    private void btAnnuleerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAnnuleerActionPerformed
    gCon.ingelogt(true);
    gCon.KlantGevonden(false, klant);
    }//GEN-LAST:event_btAnnuleerActionPerformed

    public void clearErrorLog()
    {
        lbError1.setText("");
        lberror2.setText("");
        
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Gui_KlantGevonden.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Gui_KlantGevonden.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Gui_KlantGevonden.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Gui_KlantGevonden.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Gui_KlantGevonden(klant).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtInnemen;
    private javax.swing.JButton BtReserveer;
    private javax.swing.JButton BtVerhuur;
    private javax.swing.JTable TbGgevens;
    private javax.swing.JTextField TfFilmId;
    private javax.swing.JButton btAnnuleer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JPanel lbEror3;
    private javax.swing.JLabel lbError1;
    private javax.swing.JLabel lberror2;
    // End of variables declaration//GEN-END:variables
}
